﻿using IOA.MES.Business;
using IOA.MES.DataAccess.Enum;
using IOA.MES.EsbApi.Models.Request;
using IOA.MES.EsbApi.Models.Request.Wms;
using IOA.MES.EsbApi.Models.Response;
using NLog;
using System;
using System.Linq;
using System.Web.Http;

namespace IOA.MES.EsbApi.Controllers
{
    /// <summary>
    /// WMS接口
    /// </summary>
    public class WmsController : ApiController
    {
        private Logger logger = LogManager.GetLogger(nameof(WmsController));

        /// <summary>
        /// 成品收货接口
        /// </summary>
        [HttpPost]
        public BaseResponse ComponentReceipt([FromBody] BaseRequest<ComponentReceiptRequest> request)
        {
            var receipt = request?.requestInfo?.data?.list;
            if (receipt == null || string.IsNullOrWhiteSpace(receipt.documentId))
            {
                return new BaseResponse
                {
                    esbInfo = new EsbResponseInfo("E", "E", "请至少下发一个合法单据")
                };
            }

            var status = 0;
            int.TryParse(receipt.documentStatus, out status);
            status = new[] { 2, 3 }.Contains(status) ? status : 0;

            try
            {
                //new WmsMgr().Receive(receipt.documentId.Trim(), (WmsReceiveStatus)status);
            }
            catch (Exception ex)
            {
                return new BaseResponse
                {
                    esbInfo = new EsbResponseInfo("E", "E", ex.Message)
                };
            }
            return new BaseResponse();
        }
    }
}
